package com.lhh.service;

import com.lhh.entity.User;
import com.lhh.entity.Validate;
import com.lhh.mapper.ValidateMapper;

import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class ValidateService {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    @Autowired
    private ValidateMapper validateMapper;

    @Autowired
    private static Validate validate;

    /**
     * 发送邮件：@Async进行异步调用发送邮件接口
     * @param email
     */
    @Async
    public void sendpasswordResetEmail(MimeMessage email) {
        javaMailSender.send(email);
    }

    /**
     * 在validate表中插入一条记录
     * @param validate
     * @param user
     * @param token
     * @return
     */
    public int insertNewResetRecord(Validate validate, User user, String token){
        validate.setUserId(user.getUserId());
        validate.setEmail(user.getEmail());
        validate.setToken(token);
        validate.setStatus("passwordReset");
        validate.setGmtCreate(new Date());
        validate.setGmtModified(new Date());
        return validateMapper.insert(validate);
    }
    /**通过token查找重置记录*/
    public Validate findUserByResetToken(String token) {
        return validateMapper.selectByToken(token);
    }
    /** 验证是否发送重置邮件：每个email的重置密码每日请求上限为requestPerDay次，与上一次的请求时间间隔为interval分钟
     * @param email
     * @param requestPerDay
     * @param interval
     * @return
     *

    public boolean sendValidateLimitation(String email,long requestPerDay,long interval) {
        List validateList = validateMapper.selectByEmail(email);
        if(validateList.isEmpty()){
            return true;
        }
        long countTodayValidation = validateList.stream().filter(x-> DateUtils.isSameDay(x.getGmtModified(), new Date())).count();
        Optional validate = validateList.stream().map(Validate::getGmtModified).max(Date::compareTo);
        Date dateOfLastRequest = new Date();
        if (validate.isPresent()) {
            dateOfLastRequest = (Date) validate.get();
        }
        long intervalForLastRequest = new Date().getTime() - dateOfLastRequest.getTime();
        return countTodayValidation <= requestPerDay && intervalForLastRequest >= interval *60 *1000;
    }

     */

    /**
     * 验证连接是否失效：链接有两种情况失效 1.超时 2.最近请求的一次链接自动覆盖之前的链接
     * @param email
     * @param requestPerDay
     * @param interval
     * @return

    public boolean validateLimitation(String email,long requestPerDay,long interval,String token) {
        List validateList = validateMapper.selectByEmail(email);
        Optional validateopt = validateList.stream().map(validate::getGmtModified).max(Date::compareTo);
        Date dateOfLastRequest = new Date();
        if(validateopt.isPresent()){
            dateOfLastRequest = (Date) validateopt.get();
            long intervalForLastRequest = new Date().getTime() - dateOfLastRequest.getTime();
            Optional lastRequestToken = validateList.stream().filter(x-> x.getToken().equals(token)).map(validate::getGmtModified).findAny();
            Date dateOfLastRequestToken = new Date();
            if(lastRequestToken.isPresent()) {
                dateOfLastRequestToken = (Date) lastRequestToken.get();
            }return intervalForLastRequest <= interval *60 * 1000 && dateOfLastRequest == dateOfLastRequestToken;
        }
    }
     */
}
